<template>
	<view>
		<van-form @submit="onSubmit">
			<view style="text-align: center;padding-top: 50rpx;">
				<van-rate :value="score" @change="onChangeScore" />
			</view>
			<van-field :value="text" @input="onChangeText" rows="3" autosize label="评价" type="textarea" />
			<van-uploader style="margin-left: 10rpx;" image-fit="aspectFit" :file-list="image" multiple=true @after-read="afterRead" @delete="onDelete" preview-size="225rpx" max-count="9" upload-text="添加图片" />
			<button class="button" plain @click="onSubmit">提交</button>
		</van-form>
	</view>
</template>

<script>
	export default{
		data(){
			return {
				orderId:'',
				text:'',
				score:5,
				image:[]
			}
		},
		onLoad(param){
			const that=this;
			that.orderId=param.orderId;
			wx.showLoading();
			wx.request({
				method:'POST',
				url:that.myConfig.apiUrl+'/checkReview.php',
				data:{
					orderId:that.orderId
				},
				success:function(res){
					wx.hideLoading();
					console.log('checkReview',res);
					if(res.data.find){
						that.text=res.data.text;
						that.score=res.data.score;
						for(let i=0;i<res.data.image.length;i++){
							that.image[i]={};
							that.image[i].url=res.data.image[i];
							that.image[i].status='done';
						}
					}
				},
				fail:function(){
					wx.hideLoading();
					that.myConfig.err();
				}
			});
		},
		methods:{
			//提交
			onSubmit(){
				const that=this;
				if(that.text==''){
					uni.showToast({
						title:'别忘记输入文字哦',
						icon:'none',
						duration:1200
					});
					return ;
				}
				wx.showLoading();
				let tmpImage = [];
				that.image.forEach(item => {
					if(item.status!='done'){
						uni.showToast({
							title:'别急呀，图片还在上传',
							icon:'none',
							duration:1200
						});
						return ;
					}
					tmpImage.push(item.storageUrl);
				});
				wx.request({
					url:that.myConfig.apiUrl+'/setReview.php',
					method:'POST',
					data:{
						openId:that.myConfig.openId,
						orderId:that.orderId,
						text:that.text,
						score:that.score,
						image:tmpImage
					},
					success:function(res){
						wx.hideLoading();
						console.log('setReview',res);
						if(res.data.errCode==200){
							uni.navigateBack();
							uni.showToast({
								title:'评价成功',
								icon:'none',
								duration:1200
							});
						}else{
							uni.showToast({
								title:'请先删除之前的评价',
								icon:'none',
								duration:1200
							});
						}
					},
					fail:function(){
						wx.hideLoading();
						that.myConfig.err();
					}
				})
			},
			//读取到了临时地址并上传到数据库
			afterRead(event){
				const that=this;
				for(const filetmp of event.detail.file){
					filetmp.status='uploading';
					filetmp.url=filetmp.path;
					that['image'].push(filetmp);
				}
				for(const index in that['image']){
					if(that['image'][index].status!='uploading')
						continue;
					let fileName=that.myConfig.getRandomFileName(that['image'][index].url);
					wx.cloud.uploadFile({
						cloudPath: 'review/'+fileName,
						filePath: that['image'][index].url,
						success: function(res){
							that.image[index].storageUrl=that.myConfig.storageUrl+'/review/'+fileName;
							that['image'][index].status='done';
						},
						fail: function(err){
							that.image[index].storageUrl=null;
							that['image'][index].status='failed';
						}
					});
				}
			},
			//点击删除
			onDelete(event){
				const index=event.detail.index;
				this[`image`].splice(index, 1);
			},
			//v-model
			onChangeText(value){
				const that=this;
				that.text=value.detail;
			},
			onChangeScore(value){
				const that=this;
				that.score=value.detail;
			}
		}
	}
</script>

<style scoped>
	.button{
		width: 90%;
		margin-top: 10rpx;
		margin-bottom: 10rpx;
	}
</style>
